#ifndef HEADLE_H
#define HEADLE_H

#include <my_header.h>
#include <arpa/inet.h>
#include <sys/time.h>

// 命令定义
#define CMD_UPLOAD          1
#define CMD_DOWNLOAD        2
#define CMD_DELETE          3
#define CMD_UPLOAD_RESUME   4
#define CMD_DOWNLOAD_RESUME 5

// TLV类型定义
#define TYPE_COMMAND        1
#define TYPE_FILENAME       2
#define TYPE_FILESIZE       3
#define TYPE_FILEDATA       4
#define TYPE_OFFSET         5
#define TYPE_STATUS         6

typedef struct tlv_s{
    int type;       // 数据类型
    int length;     // 数据长度
    char data[0];   // 可变长数据
} tlv_t;

int sendn(int fd, const void *buf, size_t n);

int recvn(int fd, void *buf, size_t n);

int send_tlv(int fd, int type, const void *data, size_t length);

int recv_tlv(int fd, int *type, void *buf, size_t *length);

int upload_file(int sockfd, const char *filename, int resume);

int download_file(int sockfd, const char *filename, int resume);

int delete_file(int sockfd, const char *filename);

int handle_command(int sockfd, char *command, char *file_name);

#endif
